import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Response,
  Query,
  Request,
  UnauthorizedException,
  Put,
} from '@nestjs/common';
import { UserService } from './user.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { ApiOperation } from '@nestjs/swagger';
import { LoginUserDto } from './dto/login-user.dto';
import { Public } from 'src/common/decorators/public';
import { FindUserDto } from './dto/find-user.dto';

@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) {}

  /**
   * @description 创建用户
   */
  @Post('createUser')
  @Public()
  @ApiOperation({ summary: '创建用户' })
  create(@Body() createUserDto: CreateUserDto) {
    return this.userService.create(createUserDto);
  }

  /**
   * @description 删除用户
   */
  @Delete('removeUser/:id')
  @ApiOperation({ summary: '根据id删除用户' })
  remove(@Param('id') id: string) {
    return this.userService.remove(+id);
  }

  /**
   * @description 根据id查询用户详情
   */
  @Get('findOneUser/:id')
  @ApiOperation({ summary: '根据id查询用户详情' })
  findOne(@Param('id') id: string) {
    return this.userService.findOne(+id);
  }

  /**
   * @description 分页查询用户列表
   */
  @Get('findUserListPaginated')
  @ApiOperation({ summary: '分页查询用户列表' })
  findUserListPaginated(@Query() findUserDto: FindUserDto) {
    return this.userService.findUserListPaginated(findUserDto);
  }

  /**
   * @description 更新用户信息
   */
  @Put('updateProfile')
  @ApiOperation({ summary: '更新当前用户信息' })
  async updateProfile(@Request() req, @Body() updateUserDto: UpdateUserDto) {
    const userId = req.user?.id;

    if (!userId) {
      throw new UnauthorizedException('未找到用户信息');
    }

    // 更新用户信息
    const updatedUser = await this.userService.updateProfile(
      userId,
      updateUserDto,
    );

    return {
      code: 200,
      message: '用户信息更新成功',
      data: updatedUser,
    };
  }

  /**
   * @description 根据用户ID更新用户信息
   */
  @Put('updateUser/:id')
  @ApiOperation({ summary: '根据用户ID更新用户信息' })
  async updateUser(
    @Param('id') id: string,
    @Body() updateUserDto: UpdateUserDto,
  ) {
    // 更新用户信息
    const updatedUser = await this.userService.updateUser(+id, updateUserDto);

    return {
      code: 200,
      message: '用户信息更新成功',
      data: updatedUser,
    };
  }

  /**
   * @description 获取当前登录用户信息
   */
  @Get('info')
  @ApiOperation({ summary: '获取当前登录用户信息' })
  async getCurrentUser(@Request() req) {
    // 从请求对象中获取用户ID
    const userId = req.user?.id;
    if (!userId) {
      throw new UnauthorizedException('未找到用户信息');
    }
    return this.userService.findOne(userId);
  }

  /**
   * @description 用户登录
   */
  @Post('login')
  @Public()
  @ApiOperation({ summary: '用户登录' })
  async login(@Body() loginUserDto: LoginUserDto) {
    return this.userService.login(loginUserDto);
  }

  /**
   * @description 获取图形验证码
   */
  @Post('graphicsCode')
  @Public()
  @ApiOperation({ summary: '获取图形验证码' })
  async graphicsCode() {
    return this.userService.graphicsCode();
  }

  /**
   * @description 用户退出登录
   */
  @Post('logout')
  @ApiOperation({ summary: '用户退出登录' })
  async logout(@Request() req) {
    const userId = req.user?.userId || req.user?.id;

    if (!userId) {
      throw new UnauthorizedException('未找到用户信息');
    }

    await this.userService.logout(userId);

    return {
      code: 200,
      message: '退出成功',
      timestamp: new Date().toISOString(),
    };
  }
}
